package com.meadvc.ssmdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.dao.SupplierDao;
import com.meadvc.ssmdemo.service.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: ssm-demo
 * @description:
 * @author: meadvc
 * @create: 2020-06-21 11:11
 **/
@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierDao dao;

    @Override
    public JSONObject listSuppplier( int pagenum, int pagesize) {
        List<JSONObject> list = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int offset = (pagenum-1)*pagesize;
        int total = 0;
        list = dao.getSuppliers2(offset,pagesize);
        total = dao.countSuppliers();
        if(list!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }

        data.put("total",total);
        data.put("pagenum",pagenum);
        data.put("suppliers",list);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject addSupplier(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.addSupplier(requestJson);
        if(index!=0){
            meta.put("msg","联系人添加成功");
            meta.put("status",200);
        }else{
            meta.put("msg","联系人添加失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject getSupplier(int sid) {
        JSONObject json = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        json = dao.getSupplierById(sid);
        if(json!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }
        data.put("contact",json);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject updateSupplier(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.updateSupplier(requestJson);
        if(index!=0){
            meta.put("msg","供应商修改成功");
            meta.put("status",200);
        }else{
            meta.put("msg","供应商修改失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject deleteSupplier(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.removeSupplier(requestJson);
        if(index!=0){
            meta.put("msg","供应商删除成功");
            meta.put("status",200);
        }else{
            meta.put("msg","供应商删除失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }
}
